/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  HuaTiFaBuActivity.java   
 * @Package com.jiyi.activity.main   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-26 上午10:53:39   
 * @version V1.0     
 */
package com.jiyi.activity.main;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.jiyi.activity.SinglePicSourceActivity;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.JiYouXiuListEvent;
import com.jiyi.inteface.WebApi;
import com.jiyi.widget.picselect.PicSelectConstans;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * @ClassName: HuaTiFaBuActivity
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-26 上午10:53:39
 * 
 */
public class JiYouXiuFaBuActivity extends SinglePicSourceActivity {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.gvPic)
	GridView gvPic;
	
	@BindView(R.id.etContent)
	EditText etContent;

	String id;
	LayoutInflater inflater;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jiyouxiu_fabu);
		ButterKnife.bind(this);
		inflater = LayoutInflater.from(this);
		setTop();
		refreshAdapter(gvPic);
	}
	
	private void setTop() {
		tvBack.setText("");
		tvTitle.setText("吉友秀发布");
		tvSearch.setCompoundDrawables(null, null, null, null);
		tvSearch.setText("发布");
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			// 发布
			doPost();
			break;
		default:
			break;
		}
	}

	Context getContext() {
		return this;
	}


	
	void doPost() {
		String webApi = WebApi.JIYOUXIU_FABU;
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("content", etContent.getText().toString());

		PostFormBuilder postForm = OkHttpUtils.post().url(webApi);
		postForm.params(params);
		// 图片数组
		int size = PicSelectConstans.publish_pics.size();
		File file;
		for (int i = 0; i < size; i++) {
			file = new File(PicSelectConstans.publish_pics.get(i));
//			file = ImageUtil.getImageFileFromPath(PicSelectConstans.publish_pics.get(i));
//			postForm.addFile("image"+(i+1), "pic"+(i+1)+".jpg", file);
			postForm.addFile("image[]", "pic"+(i+1)+".jpg", file);
		}

		dialog = showDialog(dialog);
		postForm.build().execute(new StringCallback() {

			@Override
			public void onError(Call arg0, Exception exception, int arg2) {
				hideDialog(dialog);
				ToastUtil.getInstances().showMsg(exception.getMessage());
			}

			@Override
			public void onResponse(String result, int arg1) {
				try {
					JSONObject obj = new JSONObject(result);
					String status = obj.getString("status");
					String info = obj.getString("info");
					if (status.equals("200")) {
						EventBus.getDefault().post(new JiYouXiuListEvent(JiYouXiuListEvent.ALL_JIYOUXIU_FRAGMENT));
						EventBus.getDefault().post(new JiYouXiuListEvent(JiYouXiuListEvent.PERSON_JIYOUXIU_FRAGMENT));
						finish();
					} 
					else if (status.equals("404") && info.contains("尚未登录")) {
						ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
						//清除环信登录
						UserHelp.getInstance().clearUser(AppBase.Context, true);
						return;
					}
					else {
						ToastUtil.getInstances().showMsg(info);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} finally {
					hideDialog(dialog);
				}
			}
		});
	}


}
